/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Entities.DBConnection;
import Entities.Jadwal;
import Entities.User;
import Entities.Studio;
import java.io.*;
import java.net.*;

import java.sql.ResultSet;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

/**
 *
 * @author JackOddie
 */
public class ControllerServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author JackOddie
     */
    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, java.io.IOException {

        String id = request.getParameter("id");
        String passWd = request.getParameter("passWd");



        if (id == null || passWd == null) {
            String url = "index.jsp";
            String action = request.getParameter("action");


            if (action != null) {
                if (action.equals("date")) {
                    url = "addForm.jsp";
                } else if (action.equals("menuUser")) {
                    url = "MenuUser.jsp";
                }else if (action.equals("DataStudio")) {
                    url = "DataStudio.jsp";
                }else if (action.equals("editStudio")) {
                    try {
                        Studio studio = new Studio(request.getParameter("jenis_studio"), request.getParameter("harga_sewa_studio"), request.getParameter("detail_studio"), request.getParameter("studio_status"));
                        studio.setId_studio(request.getParameter("id_studio"));
                        studio.updateData();
                        url = "DaftarStudio.jsp";
                    } catch (SQLException ex) {
                        Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }else if (action.equals("editPenyewa")) {
                    if(!request.getParameter("nomorTelpPenyewa").isEmpty()){
                        try {
                            Jadwal jadwal = new Jadwal();
                            jadwal.setNamaPenyewa(request.getParameter("namaPenyewa"));
                            jadwal.setNoTelpPenyewa(request.getParameter("nomorTelpPenyewa"));
                            System.out.println(jadwal.getNoTelpPenyewa());
                            jadwal.updateData(request.getParameter("kodeTelp"));
                        } catch (SQLException ex) {
                            Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    url = "addForm.jsp";
                }else if (action.equals("deletePenyewa")) {
                    if(!request.getParameter("nomorTelpPenyewa").isEmpty()){
                        try {
                            Jadwal jadwal = new Jadwal();
                            jadwal.setNoTelpPenyewa(request.getParameter("nomorTelpPenyewa"));
                            jadwal.setInactive();
                        } catch (SQLException ex) {
                            Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    url = "addForm.jsp";
                }else if (action.equals("dataPenyewa")) {
                    url = "DataPenyewa.jsp";
                } else if (action.equals("logout")) {
                    url = "index.jsp";
                }else if (action.equals("login")) {
                    url = "Login.jsp";
                }else if (action.equals("index")) {
                    url = "index.jsp";
                } else if (action.equals("studio")) {
                    url = "DaftarStudio.jsp";
                } else if (action.equals("formJadwal")) {
                    url = "FormJadwal.jsp";

                } else if (action.equals("addStudio")) {
                    String jenisStudio = request.getParameter("jenisStudio");
//                    if (request.getParameter("jenisStudio").equals("rekaman")) {
//                        jenisStudio = "rekaman";
//                    } else if (request.getParameter("jenisStudio").equals("latihan")) {
//                        jenisStudio = "latihan";
//                    }

                    String hargaSewa = request.getParameter("hargaPenyewaan");
                    String detailStudio = request.getParameter("detailStudio");
                    String statusStudio = "active";
                    if (hargaSewa == null) {
                        request.setAttribute("error", "yes");
                    } else {
                        if (Studio.isHargaSewaValid(hargaSewa) == true) {
                            try {
                                Studio studio = new Studio(jenisStudio, hargaSewa, detailStudio,statusStudio);
                                studio.saveToDB();
                            } catch (SQLException ex) {
                                Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            request.setAttribute("error", "yes");
                        }
//                        request.setAttribute("error", "yes");
                    }
                    url = "DaftarStudio.jsp";


                } else if (action.equals("addJadwal")) {

                    String nama = request.getParameter("namaPenyewa");
                    String noTelpPenyewa = request.getParameter("noTelpPenyewa");
                    String studio = request.getParameter("studio");
                    String statusLangganan = request.getParameter("status_langganan");
                    String jamMulai = request.getParameter("jamMulai");
                    String jamSelesai = request.getParameter("jamSelesai");
                    String tanggal = request.getParameter("tanggal");
                    String bulan = request.getParameter("bulan");
                    String tahun = request.getParameter("tahun");
                    String idUser = request.getParameter("idUser");

                    if (nama == null || noTelpPenyewa == null || studio == null || jamMulai == null || jamSelesai == null || tanggal == null || bulan == null) {
                        request.setAttribute("error", "yes");

                    } else {
                       
                                if (Jadwal.isNamePenyewaValid(nama) == true && Jadwal.isNomorTelponPenyewaValid(noTelpPenyewa) == true) {
                                    Calendar cal = new GregorianCalendar();
                                    String date = tanggal + "-" + bulan + "-" + tahun;
                                    Jadwal jadwal = new Jadwal(jamMulai, jamSelesai, date, statusLangganan, idUser, nama, noTelpPenyewa, studio);

                                    try {

                                        jadwal.saveToDB();
                                    } catch (SQLException ex) {
                                        Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                    

                                } else {
                                    // nama penyewa tidak valid
                                    request.setAttribute("error", "yes");
                                }

                            

                        

                    }
                    url = "addForm.jsp";
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher(url);
            
            rd.forward(request, response);
        } else {
            try {



                if (User.isExist(id, passWd)) {
                    // login sukses
                    
                    RequestDispatcher rd = request.getRequestDispatcher("MenuUser.jsp");
                    rd.forward(request, response);
                } else {
                    // login gagal
                    request.setAttribute("error", "yes");
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                }
            } catch (SQLException ex) {
                Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
            }


        }


    }
    }




